//
//  LXNetworkConfig.m
//  ProjectSource_Demo
//
//  Created by Liu on 16/3/29.
//  Copyright © 2016年 AngryBear. All rights reserved.
//

#import "LXNetworkConfig.h"

@implementation LXNetworkConfig

+ (LXNetworkConfig *)defaultConfig
{
    static LXNetworkConfig *config;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        config = [[LXNetworkConfig alloc] init];
    });
    return config;
}

- (instancetype)init {
    if (self = [super init]) {
#if DEBUG
        _type = LXURLEnvironmentTypeProduction;
#else
        _type = LXURLEnvironmentTypeProduction;
#endif
    }
    return self;
}

- (NSString *)urlWithPath:(NSString *)path {
    NSString *url;
    if (![path hasPrefix:@"/"]) {
        path = [NSString stringWithFormat:@"/%@", path];
    }
    
    if (self.type == LXURLEnvironmentTypeAWSTest) {
        //AWS测试环境地址
        NSString *baseURL = @"http://54.222.208.246"; //AWS测试环境
        NSInteger port = [self awsEvenronmentURLPortWithPath:path];
        url = [NSString stringWithFormat:@"%@:%ld%@", baseURL, port, path];
    }
    else if (self.type == LXURLEnvironmentTypeInnerTest1) {
        NSString *baseURL = [self innterTest1URLWithPath:path];
        url = [NSString stringWithFormat:@"%@%@", baseURL, path];
    }
    else if (self.type == LXURLEnvironmentTypeInnerTest2) {
        NSString *baseURL = [self innterTest2URLWithPath:path];
        url = [NSString stringWithFormat:@"%@%@", baseURL, path];
    }
    else if (self.type == LXURLEnvironmentTypeDevelopTest185) {
        NSString *baseURL = [self developEvenronment185URLWithPath:path];
        url = [NSString stringWithFormat:@"%@%@", baseURL, path];
    }
    else if (self.type == LXURLEnvironmentTypeDevelopTest184) {
        NSString *baseURL = [self developEvenronment184URLWithPath:path];
        url = [NSString stringWithFormat:@"%@%@", baseURL, path];
    }
    else {
        NSString *baseURL = [self productEvenronmentURLWithPath:path];
        url = [NSString stringWithFormat:@"%@%@", baseURL, path];
    }
    
    return url;
}

- (NSString *)h5BaseURL
{
    NSString *baseURL = @"";
    if (self.type == LXURLEnvironmentTypeAWSTest) {
        baseURL = @"http://wap-test-8889.cn-1.el.am.com.cn";
    }
    else if (self.type == LXURLEnvironmentTypeInnerTest1) {
        return @"http://172.31.75.194:81";
    }
    else if (self.type == LXURLEnvironmentTypeInnerTest2) {
        return @"http://172.31.75.194:82";
    }
    else if (self.type == LXURLEnvironmentTypeDevelopTest185 || self.type == LXURLEnvironmentTypeDevelopTest184) {
        return @"http://172.31.75.194:80";
    }
    else {
        return @"http://m.jscloud.com";
    }
    return baseURL;
}

#pragma mark - Private Methods
- (NSInteger)awsEvenronmentURLPortWithPath:(NSString *)path {
    if ([path rangeOfString:@"/supplyCenter"].length) {
        return 7000;
    }
    else if ([path rangeOfString:@"/SLink"].length) {
        return 7100;
    }
    else if ([path rangeOfString:@"/usersystem"].length) {
        return 7200;
    }
    else if ([path rangeOfString:@"/platformservice/workbench"].length) {
        return 11008;
    }
    else if ([path rangeOfString:@"/IM"].length) {
        return 7300;
    }
    else if ([path rangeOfString:@"/jpush"].length) {
        return 7400;
    }
    else if ([path rangeOfString:@"/oss/config/api"].length) {
        return 7500;
    }
    else if ([path rangeOfString:@"/oss/upload/api"].length) {
        return 7800;
    }
    else if ([path rangeOfString:@"/oss/notify/api"].length) {
        return 7600;
    }
    else if ([path rangeOfString:@"/oss/log/api"].length) {
        return 7700;
    }
    
    return 80;
}

//内测环境
- (NSString *)innterTest2URLWithPath:(NSString *)path {
    if ([path rangeOfString:@"/supplyCenter"].length) {
        return @"http://172.31.75.100:8080";
    }
    else if ([path rangeOfString:@"/ZLink"].length) {
        return @"http://172.31.75.98:8080";
    }
    else if ([path rangeOfString:@"/usersystem"].length) {
        return @"http://172.31.75.99:80";
    }
    else if ([path rangeOfString:@"/platformservice/workbench"].length) {
        return @"http://172.31.75.99:11008";
    }
    else if ([path rangeOfString:@"/IM"].length) {
        return @"http://172.31.75.99:81";
    }
    else if ([path rangeOfString:@"/jpush"].length) {
        return @"";
    }
    else if ([path rangeOfString:@"/oss/config/api"].length) {
        return @"http://172.31.75.99:19790";
    }
    else if ([path rangeOfString:@"/oss/upload/api"].length) {
        return @"http://172.31.75.99:8888";
    }
    else if ([path rangeOfString:@"/oss/notify/api"].length) {
        return @"http://172.31.75.99:19890";
    }
    else if ([path rangeOfString:@"/oss/log/api"].length) {
        return @"http://172.31.75.99:19990";
    }
    
    return @"";
}

- (NSString *)innterTest1URLWithPath:(NSString *)path {
    if ([path rangeOfString:@"/supplyCenter"].length) {
        return @"http://172.31.75.109:8081";
    }
    else if ([path rangeOfString:@"/SLink"].length) {
        return @"http://172.31.75.109:8888";
    }
    else if ([path rangeOfString:@"/usersystem"].length) {
        return @"http://172.31.75.158";
    }
    else if ([path rangeOfString:@"/platformservice/workbench"].length) {
        return @"http://172.31.75.158:11008";
    }
    else if ([path rangeOfString:@"/IM"].length) {
        return @"http://172.31.75.178:81";
    }
    else if ([path rangeOfString:@"/jpush"].length) {
        return @"";
    }
    else if ([path rangeOfString:@"/oss/config/api"].length) {
        return @"http://172.31.75.168:19790";
    }
    else if ([path rangeOfString:@"/oss/upload/api"].length) {
        return @"http://172.31.75.158:8888";
    }
    else if ([path rangeOfString:@"/oss/notify/api"].length) {
        return @"http://172.31.75.158:19890";
    }
    else if ([path rangeOfString:@"/oss/log/api"].length) {
        return @"http://172.31.75.158:19990";
    }

    return @"";
}

- (NSString *)developEvenronment185URLWithPath:(NSString *)path {
    if ([path rangeOfString:@"/supplyCenter"].length) {
        return @"http://172.31.75.185:8080";
    }
    else if ([path rangeOfString:@"/SLink"].length) {
        return @"http://172.31.75.51:8888";
    }
    else if ([path rangeOfString:@"/usersystem"].length) {
        return @"http://172.31.75.89";
    }
    else if ([path rangeOfString:@"/platformservice/workbench"].length) {
        return @"http://172.31.75.89:11008";
    }
    else if ([path rangeOfString:@"/IM"].length) {
        return @"http://172.31.75.89:81";
    }
    else if ([path rangeOfString:@"/jpush"].length) {
        return @"http://172.31.75.89:82";
    }
    else if ([path rangeOfString:@"/oss/config/api"].length) {
        return @"http://172.31.75.89:19790";
    }
    else if ([path rangeOfString:@"/oss/upload/api"].length) {
        return @"http://172.31.75.89:20000";
    }
    else if ([path rangeOfString:@"/oss/notify/api"].length) {
        return @"http://172.31.75.89:19890";
    }
    else if ([path rangeOfString:@"/oss/log/api"].length) {
        return @"http://172.31.75.89:19990";
    }
    
    return @"";
}

- (NSString *)developEvenronment184URLWithPath:(NSString *)path {
    if ([path rangeOfString:@"/supplyCenter"].length) {
        return @"http://172.31.75.184:8081";
    }
    else if ([path rangeOfString:@"/SLink"].length) {
        return @"http://172.31.75.51:8888";
    }
    else if ([path rangeOfString:@"/usersystem"].length) {
        return @"http://172.31.75.89";
    }
    else if ([path rangeOfString:@"/platformservice/workbench"].length) {
        return @"http://172.31.75.89:11008";
    }
    else if ([path rangeOfString:@"/IM"].length) {
        return @"http://172.31.75.89:81";
    }
    else if ([path rangeOfString:@"/jpush"].length) {
        return @"http://172.31.75.89:82";
    }
    else if ([path rangeOfString:@"/oss/config/api"].length) {
        return @"http://172.31.75.89:19790";
    }
    else if ([path rangeOfString:@"/oss/upload/api"].length) {
        return @"http://172.31.75.89:20000";
    }
    else if ([path rangeOfString:@"/oss/notify/api"].length) {
        return @"http://172.31.75.89:19890";
    }
    else if ([path rangeOfString:@"/oss/log/api"].length) {
        return @"http://172.31.75.89:19990";
    }
    
    return @"";
}

- (NSString *)productEvenronmentURLWithPath:(NSString *)path {
    if ([path rangeOfString:@"/supplyCenter"].length) {
        return @"https://supplycenter.jscloud.com";
    }
    else if ([path rangeOfString:@"/SLink"].length) {
        return @"https://zlinkcloud.jscloud.com";
    }
    else if ([path rangeOfString:@"/usersystem"].length) {
        return @"https://usersystem.jscloud.com";
    }
    else if ([path rangeOfString:@"/platformservice/workbench"].length) {
        return @"https://mworkbench.jscloud.com";
    }
    else if ([path rangeOfString:@"/IM"].length) {
        return @"https://im.jscloud.com";
    }
    else if ([path rangeOfString:@"/jpush"].length) {
        return @"https://jpush.jscloud.com";
    }
    else if ([path rangeOfString:@"/oss/config/api"].length) {
        return @"https://ossconfig.jscloud.com";
    }
    else if ([path rangeOfString:@"/oss/upload/api"].length) {
        return @"https://ossupload.jscloud.com";
    }
    else if ([path rangeOfString:@"/oss/notify/api"].length) {
        return @"https://ossnotify.jscloud.com";
    }
    else if ([path rangeOfString:@"/oss/log/api"].length) {
        return @"https://osslog.jscloud.com";
    }
    
    return @"";
}

@end
